<template>
	<view>
		<!-- 自定义搜索组件 -->
		<my-search @click="goSearch"></my-search>
		<view class="scroll-container">
			<!-- 左侧滑动区 -->
			<scroll-view scroll-y="true" class="container-left">
				<view
					:class="['left-item', index === active ? 'active' : '']"
					v-for="(item, index) in cateList"
					:key="index"
					@click="activeChange(index)"
				>
					{{ item.cat_name }}
				</view>
			</scroll-view>
			<!-- 右侧滑动区 -->
			<scroll-view scroll-y="true" class="container-right" :scroll-top="scrollTop">
				<!-- 二级分类 -->
				<view class="right-item" v-for="(item, index) in cateLevel2" :key="index">
					<view class="item-title">/ {{ item.cat_name }} /</view>
					<!-- 三级分类 -->
					<view class="item-list">
						<view
							class="item-list-box"
							v-for="(i, d) in item.children"
							:key="i"
							@click="goGoodsList(i)"
						>
							<view class="item-list-img">
								<image :src="i.cat_icon.replace('dev', 'web')" mode="widthFix"></image>
							</view>
							<view class="item-list-desc">{{ i.cat_name }}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import badgeMix from '@/mixins/tabbar-badeg.js';
export default {
	mixins: [badgeMix],
	data() {
		return {
			windowHeight: 0, // 当前设备可用高度
			cateList: [],
			active: 0, // 左侧是否选中
			cateLevel2: [], // 二级分类
			scrollTop: 0 // 滚动条距离顶部的距离
		};
	},
	onLoad() {
		const sysInfo = uni.getSystemInfoSync();
		this.windowHeight = sysInfo.windowHeight;
		this.getcateList();
	},
	methods: {
		// 获取分类列表
		async getcateList() {
			const { data: res } = await uni.$http.get('/api/public/v1/categories');
			if (res.meta.status !== 200) return uni.$showMsg();
			// 一级分类
			this.cateList = res.message;
			console.log(res);
			// 二级分类
			this.cateLevel2 = res.message[0].children;
		},
		// 左侧选中
		activeChange(index) {
			this.active = index;
			// 为二级分类重新赋值
			this.cateLevel2 = this.cateList[index].children;
			this.scrollTop = this.scrollTop === 0 ? 1 : 0;
		},
		// 跳转商品列表页面
		goGoodsList(i) {
			uni.navigateTo({
				url: '/subpkg/goods_list/goods_list?cid=' + i.cat_id
			});
		},
		// 跳转搜索组件
		goSearch() {
			uni.navigateTo({
				url: '/subpkg/search/search'
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.scroll-container {
	display: flex;
	// 左容器
	.container-left {
		width: 120px;
		height: 100vh;
		.left-item {
			background-color: #f7f7f7;
			line-height: 60px;
			text-align: center;
			font-size: 12px;
			&.active {
				background-color: #ffffff;
				position: relative;
				&::before {
					content: ' ';
					display: block;
					width: 3px;
					height: 30px;
					background-color: #c00000;
					position: absolute;
					top: 50%;
					left: 0;
					transform: translateY(-50%);
				}
			}
		}
	}
	// 右容器
	.container-right {
		height: 100vh;
		.right-item {
			.item-title {
				font-size: 12px;
				font-weight: bold;
				text-align: center;
				padding: 15px 0;
			}
			.item-list {
				display: flex;
				flex-wrap: wrap;
				.item-list-box {
					width: 33.33%;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					margin-bottom: 10px;
					image {
						width: 60px;
						height: 60px;
					}
					.item-list-desc {
						font-size: 13px;
					}
				}
			}
		}
	}
}
</style>
